<?php

/**
 * @property Core_Model_Store $_store
 */
class Tab_Form_Search extends YouNet_Form
{
	/* @var $_store Core_Model_Store */

	private $_store;
	protected $_element_decorators = array(
		'ViewHelper',
	);

	public function __construct($options = null)
	{
		if (isset($options['store']))
		{
			$this->_store = $options['store'];
			unset($options['store']);
		}
		parent::__construct($options);
	}

	public function init()
	{
		$keyword = $this->getTranslator()->_('keyword');

		$this->setAttrib('class', 'form_search');
		$this->setAttrib('id', 'form_search');
		
		$this->addElement('text', 'keyword', array(
			'label'		=> $keyword,
			'class'		=> 'sm_input search_keyword',
			'value'		=> $keyword,
			'onfocus'	=> "if(this.value=='$keyword') this.value='';",
			'onblur'	=> "if(this.value=='') this.value=('$keyword');",
		));


		$categories = array(0 => $this->getTranslator()->_('OPTION_ALL_CATEGORY'));

		$categories += $this->_store->getArrTopCategories();
	
		$this->addElement('select', 'category', array(
			'label' => 'Category',
			'class' => 'sm_select search_category',
			'multiOptions' => $categories
		));

		$this->addElement('submit', 'submit', array(
			'label' => 'Search',
			'class' => 'sm_button search',
		));
				
		$this->removeDecorator('DtDdWrapper');
	}

	public function populate(array $values)
	{
		if(isset($values['keyword']))
		{
			$values['keyword'] = trim($values['keyword']);
		}
		
		if (!empty($values['id_category']))
		{
			$categories = explode('-', $values['id_category']);
			$values['id_category'] = $categories[0] . '-%';
		}
		parent::populate($values);
	}
}